import React, { useEffect, useState,useImperativeHandle } from 'react'
import { message    , Table, Button,Popconfirm } from 'antd';
import { Action, AuthButton, TableCard } from 'components';
import { PlusOutlined } from '@ant-design/icons'
import { UserType_GetList,UserType_Del } from '../service'
const UserType = ({ setIsModalOpen, setAddType },ref) => {
    const [dataSource, setdataSource] = useState([])
    const [isFetching, setIsFetching] = useState(false);
    useEffect(() => {
        UserType_GetData()
    }, [])

    const UserType_GetData = () => {
        setIsFetching(true)
        UserType_GetList().then(res => {
           
            setdataSource(res.data)
            setIsFetching(false)
        })
    }
    useImperativeHandle(ref, () => ({
        UserType_GetData
    }));

    return (
        <div>
            <TableCard
                rowKey="id"
                dataSource={dataSource}
                pagination={false}
                loading={isFetching}
                onReload={UserType_GetData}
                actions={[
                    <Button
                        type="primary"
                        icon={<PlusOutlined />}
                        onClick={() => {
                            setAddType("新增使用人类型")
                            setIsModalOpen(true)
                        }}
                    >新建</Button>
                ]}
            >
                <Table.Column title="使用人" dataIndex="user_type" />
                <Table.Column title="简称" dataIndex="short_name" />

                <Table.Column width={80} title="操作" render={info => (
                    <Action>

                        
                        <Popconfirm placement="top" title={`是否删除类型${info.user_type}`} onConfirm={() => {
                            UserType_Del(info.id).then((res) => {
                                console.log(res)

                                if (res.code == 0) {
                                    message.success("删除成功")
                                   UserType_GetData()
                                } else {
                                    message.error("删除失败")
                                }

                            })
                        }} okText="删除" cancelText="取消">
                            <Action.Button danger


                            >删除</Action.Button>
                        </Popconfirm>

                    </Action>
                )} />
            </TableCard>
        </div>
    )
}

export default React.forwardRef(UserType)